<template>
  <div class="manage">
    <el-dialog
      title="新增用户"
      :visible.sync="dialogVisible"
      :before-close="handleClose"
      width="50%"
    >
      <!-- <span>这是一段信息</span> -->
      <!-- 表单内容 -->
      <el-form
        ref="form"
        :rules="rules"
        :inline="true"
        :model="form"
        label-width="100px"
      >
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名"></el-input>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="form.age" placeholder="请输入年龄"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择性别">
            <el-option label="男" :value="1"></el-option>
            <el-option label="女" :value="0"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="出生日期" prop="birth">
          <el-date-picker
            v-model="form.birth"
            type="date"
            placeholder="选择日期"
            value-format="yyyy-MM-DD"
          >
          </el-date-picker>
        </el-form-item>

        <el-form-item label="地址" prop="addr">
          <el-input v-model="form.addr" placeholder="请输入地址"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
    <div class="manage-header">
      <el-button type="primary" @click="handleAdd" round>+ 新增</el-button>
      <!-- form搜索区域 -->
      <el-form :inline="true" :model="userForm" class="search">
        <el-form-item>
          <el-input v-model="userForm.name" placeholder="请输入名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" round>查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="common-tabel">
      <el-table stripe :data="tableData" style="width: 100%" height="500">
        <el-table-column prop="name" label="姓名"> </el-table-column>
        <el-table-column prop="age" label="年龄"> </el-table-column>
        <el-table-column prop="birth" label="出生日期"> </el-table-column>
        <el-table-column prop="sex" label="性别">
          <template slot-scope="scope">
            <span>
              {{ scope.row.sex === 1 ? "男" : "女" }}
            </span>
          </template>
        </el-table-column>

        <el-table-column prop="addr" label="地址"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button size="mini" type="danger" @click="handleDel(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="page">
        <!-- current-change 当页数改变时触发 -->
        <el-pagination
          layout="prev, pager, next"
          :total="total"
          @current-change="handlePage"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
// 引入列表数据
import { getUser, userAdd, userEdit, userDel } from "../api";
export default {
  name: "User",
  data() {
    return {
      dialogVisible: false,
      // 表达数据
      form: {
        name: "",
        age: "",
        sex: "",
        birth: "",
        addr: "",
      },
      rules: {
        name: [{ required: true, message: "请输入姓名" }],
        age: [{ required: true, message: "请输入年龄" }],
        sex: [{ required: true, message: "请选择性别" }],
        birth: [{ required: true, message: "请选择日期" }],
        addr: [{ required: true, message: "请输入地址" }],
      },
      tableData: [],
      modalType: 0, //  0表示新增  1表示编辑
      // 当前的总条数 total
      total: 0,
      // 分页器
      pageData: {
        page: 1,
        limit: 10,
      },
      userForm: {
        name:''
      }
    };
  },
  methods: {
    // 点击×号提示
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    // 提交用户订单
    submit() {
      // validate方法 清空表单数据
      this.$refs.form.validate((valid) => {
        // console.log(valid, "valid")
        if (valid) {
          console.log(this.form, "form");
          // 新增用户
          if (this.modalType === 0) {
            userAdd(this.form).then(() => {
              // 重新获取列表接口
              this.getUser();
            });
          } else {
            userEdit(this.form).then(() => {
              this.getUser();
            });
          }
          // 清空表单
          this.$refs.form.resetFields();
          // 关闭弹窗
          this.dialogVisible = false;
        }
      });
    },
    // 弹窗关闭的时候
    handleClose() {
      // 清空表单  element里的form表单的清空表达的方法resetFields
      this.$refs.form.resetFields();
      // 关闭弹窗
      this.dialogVisible = false;
    },
    // 取消按钮
    close() {
      // 调用handleClode
      this.handleClose();
    },
    // 新增接口
    handleAdd() {
      this.modalType = 0;
      this.dialogVisible = true;
    },
    // 编辑的按钮
    handleEdit(row) {
      this.modalType = 1;
      this.dialogVisible = true;
      this.form = JSON.parse(JSON.stringify(row));
    },
    // 删除的按钮
    handleDel(row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        userDel({ id: row.id })
          .then(() => {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            // 重新获取端口
            this.getUser();
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
          });
      });
    },
    // 获取列表数据
    getUser() {
      getUser({params:{ ...this.userForm, ...this.pageData} }).then(({ data }) => {
        console.log(data);
        this.tableData = data.list;
        // 获取
        this.total = data.count || 0;
      });
    },
    // 选择页码的回调
    handlePage(val) {
      // console.log(val);
      this.pageData.page = val;
      this.getUser();
    },
    goSearch() {
      this.getUser()
    }
  },
  mounted() {
    this.getUser();
  },
};
</script>

<style lang="less" scoped>
.manage {
  height: 90%;
  .common-tabel {
    position: relative;
    height: calc(100% - 62px);
    .page {
      position: absolute;
      bottom: -50PX;
      right: 20px;
    }
  }
  .manage-header {
    display: flex;
    justify-content: space-between;
    .el-button.is-round{
      height: 36px;
    }
  }
}
</style>